package com.taotao.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import Utils.ObjectResult;

import com.github.pagehelper.PageInfo;
import com.taotao.pojo.Item;
import com.taotao.pojo.ItemDesc;
import com.taotao.service.ItemService;

@Controller
public class ItemController {

	@Autowired
	private ItemService service;

	/**
	 * 新增商品
	 * 
	 * @param item
	 * @param desc
	 * @return
	 */
	@RequestMapping(value = "item", method = RequestMethod.POST)
	public ResponseEntity<Void> inserItem(Item item,
			                              @RequestParam("desc") String desc,
			                              @RequestParam("itemParams")String params) {
		if (StringUtils.isEmpty(item.getTitle())) {
			return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
		}
		try {
			service.saveItem(item, desc, params);
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {

			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.build();
		}
	}

	/**
	 * 查询商品列表
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value = "item", method = RequestMethod.GET)
	public ResponseEntity<ObjectResult> queryItem(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "30") Integer rows) {

		try {
			PageInfo<Item> info = service.queryPages(page, rows);
			ObjectResult objectResult = new ObjectResult(info.getTotal(),
					info.getList());
			return ResponseEntity.ok(objectResult);
		} catch (Exception e) {
			// TODO: handle exception
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body(null);
		}
	}
	
	/**
	 * 根据id查询商品
	 * @param itemId
	 * @return
	 */
	@RequestMapping(value = "item/{itemId}", method = RequestMethod.GET)
	public ResponseEntity<Item> queryItemById(
			@PathVariable("itemId")Long itemId) {
		try {
			 
			Item item = service.queryById(itemId);
			if(null == item){
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(item);
		} catch (Exception e) {
			// TODO: handle exception
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body(null);
		}
	}

	/**
	 * 逻辑删除商品，将status改为3即下架状态
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "item", method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteItem(@RequestParam("ids") List<Object> ids) {

		try {
			service.updateByIds(ids);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {

			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.build();
		}
	}

	@RequestMapping(value = "item", method = RequestMethod.PUT)
	public ResponseEntity<Void> updateItem(Item item,
			@RequestParam("desc") String desc) {
		try {
			service.updateItem(item, desc);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

}
